/**
 * 
 */
package com.xjt.common;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @author Jet Xu
 */
public class XmlUtils {

    /** 日志类 */
    private static Logger log = Logger.getLogger(XmlUtils.class);

    @SuppressWarnings("unchecked")
    public static <T> List<T> getListWithConfig(String cfgPath, String rootName, String entityName, Class<T> clz) {
        List<T> list = new ArrayList<T>();

        String xml;
        try {
            xml = FileUtils.getFileContent(new File(SystemInfo.getClassPath(), cfgPath));
        } catch (IOException e) {
            log.error("", e);
            xml = null;
        }

        if (StringUtils.isEmpty(xml)) {
            return list;
        }

        XStream xs = new XStream(new DomDriver());
        xs.alias(rootName, List.class);
        xs.alias(entityName, clz);

        list = (List<T>) xs.fromXML(xml);

        return list;
    }

    /**
     * @param cfgPath
     * @param rootName
     * @param entityName
     * @param clz
     * @param key
     * @return
     * @deprecated
     */
    public static <T> Map<String, T> getMapWithConfig_old(String cfgPath, String rootName, String entityName,
                                                          Class<T> clz, String key) {
        // 取得字段的get方法名
        String getMethodName = StringUtils.getGetMethodName(key);
        Method getMethod = null;

        try {
            Object obj = clz.newInstance();
            // 取得get方法
            getMethod = obj.getClass().getMethod(getMethodName);
        } catch (InstantiationException e) {
            log.error("", e);
        } catch (IllegalAccessException e) {
            log.error("", e);
        } catch (SecurityException e) {
            log.error("", e);
        } catch (NoSuchMethodException e) {
            log.error("", e);
        }

        // 如果方法不存在，则直接返回
        if (null == getMethod) {
            return null;
        }

        List<T> list = getListWithConfig(cfgPath, rootName, entityName, clz);

        Map<String, T> map = new HashMap<String, T>();

        for (T t : list) {
            try {
                String keyVlaue = String.valueOf(getMethod.invoke(t));
                map.put(keyVlaue, t);
            } catch (IllegalArgumentException e) {
                log.error("", e);
                return null;
            } catch (IllegalAccessException e) {
                log.error("", e);
                return null;
            } catch (InvocationTargetException e) {
                log.error("", e);
                return null;
            }
        }

        return map;
    }

    public static <T> Map<String, T> getMapWithConfig(String cfgPath, String rootName, String entityName, Class<T> clz,
                                                      String key) {

        List<T> list = getListWithConfig(cfgPath, rootName, entityName, clz);

        Map<String, T> map = new HashMap<String, T>();

        for (T t : list) {
            BeanWrapper bw = new BeanWrapperImpl(t);

            try {
                String keyVlaue = String.valueOf(bw.getPropertyValue(key));
                map.put(keyVlaue, t);
            } catch (Throwable ex) {
                return null;
            }
        }

        return map;
    }

}
